package com.smt.take.jpush

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Context.NOTIFICATION_SERVICE
import android.content.Intent
import android.os.Build
import android.util.Log
import androidx.core.app.NotificationCompat
import cn.jpush.android.api.CustomMessage
import cn.jpush.android.api.JPushMessage
import cn.jpush.android.api.NotificationMessage
import cn.jpush.android.service.JPushMessageReceiver
import com.smt.take.R
import com.smt.take.ui.activity.MainActivity


/**
 *描述: 对推送进行处理
 */
class MyReceiver : JPushMessageReceiver() {
    /**
     * TODO 连接极光服务器
     */
    override fun onConnected(context: Context?, b: Boolean) {
        super.onConnected(context, b)
        Log.d("连接极光服务器", "onConnected")
    }

    /**
     * TODO 注册极光时的回调
     */
    override fun onRegister(context: Context?, s: String) {
        super.onRegister(context, s)
        Log.d("注册极光时的回调", "onRegister$s")
    }

    /**
     * TODO 注册以及解除注册别名时回调
     */
    override fun onAliasOperatorResult(context: Context?, jPushMessage: JPushMessage) {
        super.onAliasOperatorResult(context, jPushMessage)
        Log.d("注册以及解除注册别名时回调", jPushMessage.toString())
    }

    /**
     * TODO 接收到推送下来的通知
     * 可以利用附加字段（notificationMessage.notificationExtras）来区别Notication,指定不同的动作,附加字段是个json字符串
     * 通知（Notification），指在手机的通知栏（状态栏）上会显示的一条通知信息
     */
    override fun onNotifyMessageArrived(
        context: Context?,
        notificationMessage: NotificationMessage
    ) {
        super.onNotifyMessageArrived(context, notificationMessage)
        Log.d("接收到推送下来的通知", notificationMessage.toString())
    }

    /**
     * TODO 打开了通知
     * notificationMessage.notificationExtras(附加字段)的内容处理代码
     * 比如打开新的Activity， 打开一个网页等..
     */
    override fun onNotifyMessageOpened(
        context: Context,
        notificationMessage: NotificationMessage
    ) {
        super.onNotifyMessageOpened(context, notificationMessage)
        Log.d("打开了通知", notificationMessage.notificationExtras)
    }

    /**
     * TODO 接收到推送下来的自定义消息
     * 自定义消息不是通知，默认不会被SDK展示到通知栏上，极光推送仅负责透传给SDK。其内容和展示形式完全由开发者自己定义。
     * 自定义消息主要用于应用的内部业务逻辑和特殊展示需求
     */
    override fun onMessage(context: Context, customMessage: CustomMessage) {
        super.onMessage(context, customMessage)
        // 收到消息 显示通知
        Log.d(
            "接收到推送下来的自定义消息--->",
            "onMessage:----> " + "{messageId =" + customMessage.messageId + ", extra='" + customMessage.extra + ", message='" + customMessage.message + ", contentType='" + customMessage.contentType + ", title='" + customMessage.title + ", senderId='" + customMessage.senderId + ", appId='" + customMessage.appId + '}'
        )
        processCustomMessage(context, customMessage)
    }

    //通知
    private fun processCustomMessage(
        context: Context,
        customMessage: CustomMessage
    ) {
        val channelID = "1"
        val channelName = "channel_name"

        // 跳转的Activity
        val intent = Intent(context, MainActivity::class.java)
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
        val pendingIntent = PendingIntent.getActivity(context, 0, intent, 0)
        val notificationManager =
            context.getSystemService(NOTIFICATION_SERVICE) as NotificationManager

        //适配安卓8.0的消息渠道
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel =
                NotificationChannel(
                    channelID,
                    channelName,
                    NotificationManager.IMPORTANCE_HIGH
                )
            notificationManager.createNotificationChannel(channel)
        }
        val notification: NotificationCompat.Builder =
            NotificationCompat.Builder(context, channelID)
        notification.setAutoCancel(true)
            .setContentText(customMessage.message)
            .setContentTitle(customMessage.title)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setDefaults(Notification.DEFAULT_ALL)
            .setContentIntent(pendingIntent)
        notificationManager.notify(
            (System.currentTimeMillis() / 1000).toInt(),
            notification.build()
        )
    }


}